<template>
	<view class="ljapp doctor">
		<view class="dev">
			<view class="header" :style="'background-image: url('+$base.cdn('/uploads/20220408/ea676fac98b92b4d7b3172b6256a86af.png')+');'">
				<u-navbar placeholder leftIconColor="#fff" titleStyle ="color:#fff;" title="医生端" :bgColor="'rgba(53,107,253,'+navbarBgAlpha+')'" @leftClick="goto('/pages/my/change')"></u-navbar>
				<view class="outer">
					<view class="userinfo u-flex u-flex-y-center">
						<image class="avatar" :src="$base.cdn(doctorobj?doctorobj.img:'','/static/lj/common/default-doctor.png')" mode="aspectFill"></image>
						<view class="u-flex u-flex-fill ">
							<view class="info u-flex-fill">
								<view class="nickname">你好！{{doctorobj?doctorobj.name:''}}欢迎回来</view>
								<view class="u-flex">
									<view class="date">{{nowDate}}</view>
									<view class="week">{{nowWeek}}</view>
								</view>
							</view>
							<navigator hover-class="none" url="/pagesD/doctor/wallet" class="btn-capsule u-flex u-flex-center u-flex-y-center">
								<image src="/static/lj/doctor/wallet.png" mode="aspectFill"></image>
								<view>钱包</view>
							</navigator>
						</view>
					</view>

					<view class="menu">
						<view class="inner u-flex u-flex-between">
							<navigator hover-class="none" class="item" url="/pagesD/doctor/textpage">
								<image src="/static/lj/doctor/group_735.png" mode="aspectFill"></image>
								<view>图文问诊</view>
								<view class="badge" v-if="parseInt(doctorobj.consultation_count_normal)>0">{{doctorobj.consultation_count_normal}}</view>
							</navigator>
							<navigator hover-class="none" class="item" url="/pagesD/doctor/videopage">
								<image src="/static/lj/doctor/group_610.png" mode="aspectFill"></image>
								<view>视频问诊</view>
								<view class="badge" v-if="parseInt(doctorobj.consultation_viedo_count_normal)>0">{{doctorobj.consultation_viedo_count_normal}}</view>
							</navigator>
							<navigator hover-class="none" class="item" url="/pagesA/prescript/list1">
								<image src="/static/lj/doctor/icon_diagnosisofsingle.png" mode="aspectFill"></image>
								<view>处方模板</view>
								<!-- <view class="badge">8</view> -->
							</navigator>
							<navigator hover-class="none" class="item" url="/pagesD/doctor/reportpage">
								<image src="/static/lj/doctor/icon_checklist.png" mode="aspectFill"></image>
								<view>报告解读</view>
								<view class="badge" v-if="parseInt(doctorobj.consultation_report_count_normal)>0">{{doctorobj.consultation_report_count_normal}}</view>
							</navigator>
							<navigator hover-class="none" class="item" url="/pagesD/doctor/myArticleList">
								<image src="/static/lj/doctor/group_802.png" mode="aspectFill"></image>
								<view>我的文章</view>
								<!-- <view class="badge">8</view> -->
							</navigator>
						</view>
					</view>
				</view>
			</view>

			<view class="content">
				<view class="panel u-flex u-flex-between">
					<view class="item u-flex u-flex-y-center">
						<view class="tit">图文问诊</view>
						<view class="u-flex-1 u-flex u-flex-between">
							<navigator hover-class="none" class="numbox" url="/pagesD/doctor/textpage?funnel=1">
								<view class="num">{{doctorobj.day_consultation_count}}</view>
								<view class="cont">今日人数</view>
							</navigator>
							<navigator hover-class="none" class="numbox" url="/pagesD/doctor/textpage?funnel=2">
								<view class="num">{{doctorobj.history_consultation_count}}</view>
								<view class="cont">历史人数</view>
							</navigator>
						</view>
					</view>
					<view class="item u-flex u-flex-y-center bg1">
						<view class="tit">视频问诊</view>
						<view class="u-flex-1 u-flex u-flex-between">
							<navigator hover-class="none" class="numbox" url="/pagesD/doctor/videopage?funnel=1">
								<view class="num">{{doctorobj.day_consultation_viedo_count}}</view>
								<view class="cont">今日人数</view>
							</navigator>
							<navigator hover-class="none" class="numbox" url="/pagesD/doctor/videopage?funnel=2">
								<view class="num">{{doctorobj.history_consultation_viedo_count}}</view>
								<view class="cont">历史人数</view>
							</navigator>
						</view>
					</view>
				</view>
				<view class="panel u-flex u-flex-between" style="padding-top: 0;">
					<view class="item u-flex u-flex-y-center bg2">
						<view class="tit">我的开方</view>
						<view class="u-flex-1 u-flex u-flex-between">
							<navigator hover-class="none" class="numbox" url="/pagesA/prescript/list2?funnel=1">
								<view class="num">{{doctorobj.day_doctor_prescription_count}}</view>
								<view class="cont">今日人数</view>
							</navigator>
							<navigator hover-class="none" class="numbox" url="/pagesA/prescript/list2?funnel=2">
								<view class="num">{{doctorobj.history_doctor_prescription_count}}</view>
								<view class="cont">历史人数</view>
							</navigator>
						</view>
					</view>
					<view class="item u-flex u-flex-y-center bg3">
						<view class="tit">签约患者</view>
						<view class="u-flex-1 u-flex u-flex-between">
							<navigator hover-class="none" class="numbox" url="/pagesD/doctor/mySignPatient?funnel=1">
								<view class="num">{{doctorobj.day_consultation_sign_count}}</view>
								<view class="cont">今日人数</view>
							</navigator>
							<navigator hover-class="none" class="numbox" url="/pagesD/doctor/mySignPatient?funnel=2">
								<view class="num">{{doctorobj.history_consultation_sign_count}}</view>
								<view class="cont">历史人数</view>
							</navigator>
						</view>
					</view>
				</view>

				<view class="px20">
					<view class="card">
						<view class="title u-flex u-flex-between">
							<view class="u-flex-fill">接诊大厅</view>
							<view class="ricon u-flex" @click="goto('/pagesD/doctor/receptionHall')">
								<view>全部</view>
								<u-icon name="arrow-right" color="#888888"></u-icon>
							</view>
						</view>
						
						<swiper class="lj-consultation-list"
							v-if="grabPatientList.length>0"
							:circular="true"
							:vertical="true"
							:autoplay="true"
							interval="8000"
							duration="1000"
							:disable-touch="true"
							
							style="margin-top: 20rpx;height: 224rpx;"
						>
							<swiper-item style="height: 224rpx;" v-for="(item,gpindex) in grabPatientList" :key="gpindex">
								<view class="item" style="height: 224rpx;margin-top: 0;margin-bottom: 10rpx;">
									<view class="info u-flex">
										<image class="avatar" :src="$base.cdn(item.order_info.user_headimgurl)" mode="aspectFill"></image>
										<view class="u-flex-fill">
											<view class="tit u-flex u-flex-between u-flex-y-center">
												<view class="u-flex u-flex-fill">
													<view class="name">{{item.order_info.user_the_patient_info.name}}</view>
													<view class="sex">{{item.order_info.user_the_patient_info.sex==1?'男':'女'}}</view>
													<view class="age">{{item.order_info.user_the_patient_info.age}}岁</view>
													<view class="tag" v-if="item.type=='tw'">图文</view>
													<view class="tag" v-else-if="item.type=='sp'">视频</view>
													<view class="tag" v-else-if="item.type=='report'">报告</view>
												</view>
												<view class="price">￥{{item.order_info.money}}</view>
											</view>
											<view class="con u-line-2">
												<text>病情描述</text>：{{item.type=='report'?'报告解读':item.order_info.content}}
											</view>
											<view class="u-flex u-flex-between u-flex-y-center">
												<view class="time">
													<text>下单日期</text>：{{$u.timeFormat(item.order_info.createtime,'yyyy年mm月dd日 hh:MM')}}
												</view>
												<view class="btn-img">
													<image src="/static/lj/common/capsule_1.png" mode="aspectFill"></image>
													<view class="tit" @click.stop="receivePatient(item)">立即接诊</view>
												</view>
											</view>
										</view>
									</view>
								</view>
							</swiper-item>
						</swiper>
							
						<view v-else style="text-align: center;color:#888;padding: 20rpx 0 30rpx;">暂无患者</view>
					</view>

					<view class="card">
						<view class="title u-flex u-flex-between">
							<view class="u-flex-fill">今日已接订单</view>
							<view class="ricon u-flex" @click="goto('/pagesD/doctor/orderpage')">
								<view>全部</view>
								<u-icon name="arrow-right" color="#888888"></u-icon>
							</view>
						</view>
						<view class="lj-tabs u-flex">
							<view class="item info">全部({{all_today_count}})</view>
							<view class="item danger">待接诊({{normal_today_count}})</view>
							<view class="item success">已接诊({{patient_today_count}})</view>
						</view>
						<view class="lj-consultation-list">
							<view v-if="today_list.length<=0" style="text-align: center;padding: 20rpx 0;color:#666;">今日暂无订单</view>
							<view class="item" v-for="(item,index) in today_list" :key="index">
								<view class="info u-flex">
									<image class="avatar" :src="$base.img(item.user_headimgurl)" mode="aspectFill"></image>
									<view class="u-flex-1">
										<view class="tit u-flex u-flex-between u-flex-y-center">
											<view class="u-flex u-flex-fill">
												<view class="name">{{item.user_the_patient_info.name}}</view>
												<view class="sex">{{item.user_the_patient_info.sex==1?'男':'女'}}</view>
												<view class="age">{{item.user_the_patient_info.age}}岁</view>
												<view class="tag" :class="{v:item.type=='sp', r:item.type=='report'}">
													<block v-if="item.type=='tw'">图文</block>
													<block v-else-if="item.type=='sp'">视频</block>
													<block v-else-if="item.type=='report'">报告</block>
												</view>
											</view>
											<view class="price">￥{{item.money}}</view>
										</view>
										<view class="con u-line-2">
											<view v-if="item.type!='report'"><text>病情描述</text>：{{item.content}}</view>
										</view>
										<view class="u-flex u-flex-between u-flex-y-center">
											<view class="time u-flex-1">
												<text>下单日期</text>：{{$u.timeFormat(item.createtime,'yyyy年mm月dd日 hh:MM')}}</view>
											<view class="btn-img btn-receive">
												<image src="/static/lj/common/capsule_317.png" mode="aspectFill"></image>
												<view class="tit" @click="Onjump(item)">{{item.typename}}</view>
											</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>

			<ComTabBarDoc :selected="0"></ComTabBarDoc>
		</view>
		
		<u-popup :show="show" :round="8" mode="center">
			<view class="popup-submit-content u-flex u-flex-y-center">
				<image src="/static/lj/common/thumbs.png" mode="aspectFill"></image>
				<view class="u-flex-fill">
					<view class="tit">接诊成功！</view>
					<view class="con">（{{popupSubmitTime}}s后自动关闭）</view>
				</view>
			</view>
		</u-popup>
	</view>

</template>

<script>
	import ComTabBarDoc from '@/components/ComTabBarDoc.vue';
	export default {
		components: {
			ComTabBarDoc
		},
		data() {
			return {
				navbarBgAlpha: 0,
				userInfo: {}, //用户信息
				doctorobj: {},

				nowDate: '', //年月日
				nowWeek: '', //周几

				show: false,
				popupSubmitTimer: null,
				popupSubmitTime: 5,
				timer: null,
				grabPatientList: [],
				
				all_today_count: 0,
				normal_today_count: 0,
				patient_today_count: 0,
				today_list: [],
			}
		},
		onLoad() {
			var that = this
			uni.hideTabBar()
			
			this.doctorobj = this.$store.state.docObj
			
			that.getGrabPatientList()
			this.timer = setInterval(function(){
				that.getGrabPatientList()
			}, 5000)
		},
		onUnload(){
			if(this.timer != null){
				clearInterval(this.timer)
				this.timer = null
			}
		},
		onShow() {
			if(!this.$store.state.IsOpen){
				uni.showLoading({
					title: '正在连接服务器...',
					mask: true
				});
			}
			uni.$on("onImStatus",(rel)=>{
			    console.log('==>>我被全局通知了onImStatus', rel)
				
				if(!rel.IsOpen){
					uni.showLoading({
						title: '正在连接服务器...',
						mask: true
					});
				}else{
					uni.hideLoading()
				}
			})
			
			this.Oninfo()
			this.Ontime()
		},
		onHide(){
			uni.$off("onImStatus")
		},
		onPageScroll: function(e) {
			var a = e.scrollTop/80
			this.navbarBgAlpha = a>1?1:a
		},
		methods: {
			Oninfo() {
				let that = this

				this.$Http({
					url: 'getUserInfo',
					type: 'POST',
					data: {}
				}).then(res => {
					if (res.code == 1) {
						this.userInfo = res.data
						if(!this.userInfo.doctor_info){
							uni.reLaunch({
								url: '/pagesD/doctor/apply'
							})
						}
						this.$store.commit('changeDocObj', this.userInfo.doctor_info)
						this.doctorobj = this.$store.state.docObj
					}
				})
				
				this.$Http({
					url: 'doctorlistConsultation3OrderCount',
					data: {}
				}).then(res => {
					if (res.code == 1) {
						this.all_today_count = res.data.all_today_count
						this.normal_today_count = res.data.normal_today_count
						this.patient_today_count = res.data.patient_today_count
					}
				})
				
				this.$Http({
					url: 'doctorlistConsultation3Order',
					data: {}
				}).then(res => {
					if (res.code == 1) {
						var list = res.data
						
						list.forEach(item => {
							if(item.user_the_patient_info) item.user_the_patient_info.age = this.$base.Onage(item.user_the_patient_info.birthday)
							
							if (item.pay_status == 1) {
								item.status_is = true
								if (item.status == 'normal') {
									item.typename = '去接诊'
								}
								if (item.status == 'refund') {
									item.typename = '已退款'
									item.status_is = false
								}
								if (item.status == 'refuse_patient') {
									item.typename = '拒绝接诊'
								}
								if (item.status == 'patient') {
									item.typename = '接诊中'
								}
								if (item.status == 'end'||item.status == 'comment') {
									item.typename = '接诊完成'
								}
							}
						})
						
						this.today_list = list
					}
				})
			},
			//获取时间
			Ontime() {
				let date = new Date()
				// 获取日期：年月日
				const year = date.getFullYear()
				const month = date.getMonth() + 1
				const day = date.getDate()
				this.nowDate = year + "年" + month + "月" + day + "日"
				// 获取星期几
				const weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
				this.nowWeek = weeks[new Date().getDay()];
				console.log(this.nowDate, this.nowWeek)
			},
			goto(e){
				uni.navigateTo({
					url: e
				})
			},

			getGrabPatientList(){
				var that = this
				this.$Http({
					url: 'doctorGrabPatientList',
					data: {
						page: 1,
						limit: 10,
					},
					show: false,
					load: false,
					type:'GET',
				}).then(res=>{
					if(res.code == 1){
						that.grabPatientList = res.data.data || []
					}else{
						that.grabPatientList = []
					}
				})
			},
			getGrabPatientInfo(item, index){
				var that = this
				that.$Http({
					url: 'doctorGrabPatientOne',
					show: false,
					load: false,
					data: {
						id: item.id
					}
				}).then(res=>{
					if(res.code == 1){
						if(!res.data) that.grabPatientList.splice(index,i)
						that.$forceUpdate()
					}else{
						that.grabPatientList.splice(index,i)
					}
				})
			},
			// 接诊
			receivePatient(grabPatientInfo){
				var that = this, id = grabPatientInfo.id
				
				if(!this.$store.state.IsOpen){
					uni.showLoading({
						title: '正在连接服务器...',
						mask: true
					});
					return false
				}

				uni.showModal({
					title: '操作提示',
					confirmText: '确定',
					confirmColor: '#6bcbb6',
					cancelText: '取消',
					content: '您确定要接诊吗',
					success: function(res) {
						if (res.confirm) {
							that.$Http({
								url: 'consultationQuickPatient',
								type: 'POST',
								show: true,
								data: {id: id}
							}).then(res => {
								if(res.code==1){
									var cate = 'text', orderno = grabPatientInfo.order_info.order_code
									if(grabPatientInfo.type=='sp') cate = 'video'
									else if(grabPatientInfo.type=='report') cate = 'report'
									
									var FromUser = {
										prefix: "HZ",
										uid: grabPatientInfo.order_info.user_id,
										nick: grabPatientInfo.order_info.user_the_patient_info.name,
										avatar: that.$base.cdn(grabPatientInfo.order_info.user_headimgurl)
									}
									var ToUser = {
										prefix: "DT",
										uid: that.userInfo.id,
										nick: that.doctorobj.name,
										avatar: that.$base.cdn(that.doctorobj.img)
									}
									that.$store.commit('createToUser', {FromUser:ToUser, ToUser:FromUser, orderno:orderno})

									var chatList = []
									if(cate == 'report'){
										chatList.push({
											cate: cate,
											orderno: orderno,
											fromType: 'user',
											from: FromUser,
											to: ToUser,
											type: 'text',
											content: {
												text: "您好，这是我的检测报告<br>麻烦帮忙看一下，谢谢！"
											},
											time: grabPatientInfo.order_info.pay_time
										})
										chatList.push({
											cate: cate,
											orderno: orderno,
											fromType: 'user',
											from: FromUser,
											to: ToUser,
											type: 'file',
											content: {
												type: 'pdf',
												name: grabPatientInfo.order_info.report_filename,
												url: grabPatientInfo.order_info.report_url,
												time: grabPatientInfo.order_info.report_time
											},
											time: grabPatientInfo.order_info.pay_time
										})
									}else{
										chatList.push({
											cate: cate,
											orderno: orderno,
											fromType: 'user',
											from: FromUser,
											to: ToUser,
											type: 'textCard',
											content: {
												list: [
													{name:"咨询类型", value:grabPatientInfo.order_info.consultation_consult_name},
													{name:"病情描述", value:grabPatientInfo.order_info.content},
													// {name:"就诊情况", value:this.orderInfo.content},
												]
											},
											time: grabPatientInfo.order_info.pay_time//createtime
										})
										
										if(grabPatientInfo.order_info.images){
											var images = grabPatientInfo.order_info.images.toString().split(","), imgs = [], num = images.length
											for(var i in images){
												if(images[i]){
													imgs.push({
														url: images[i],
														w: 430/(num-1)
													})
												}
											}
											if(imgs.length>0){
												chatList.push({
													cate: cate,
													orderno: orderno,
													fromType: 'user',
													from: FromUser,
													to: ToUser,
													type: 'imgs',
													content: {
														list: imgs
													},
													time: grabPatientInfo.order_info.pay_time//createtime
												})
											}
										}
									}
									
									that.popupSubmitTime = 5
									that.show = true
									grabPatientInfo = null
									// 向患者发送接诊通知
									that.$store.dispatch('sendChatMessage',{
										cate: cate,
										orderno: orderno,
										type: 'consultationHandle',
										content:{
											code: "receivePatient",
											orderStatus: 1,
											patient_timing: res.data,
											chatList: chatList,
										},
									})
									that.$store.commit('closeToUser')
										
									that.getGrabPatientList()

									that.popupSubmitTimer = setInterval(function(){
										that.popupSubmitTime--
										if(that.popupSubmitTime<=0){
											that.show = false
											if(that.popupSubmitTimer!=null){
												clearInterval(that.popupSubmitTimer)
												that.popupSubmitTimer = null
											}
										
											uni.navigateTo({
												url: '/pagesA/message/chat?isDoc=1&cate='+cate+'&orderno='+orderno
											})
										}
									},1000)
								}
							})
						}
					},
				})

			},
			Onjump(e) {
				if(e.type=='tw'){
					uni.navigateTo({
						url: '/pagesA/message/chat?isDoc=1&cate=text&orderno='+e.order_code
					})
				}else if(e.type=='sp'){
					uni.navigateTo({
						url: '/pagesA/message/chat?isDoc=1&cate=video&orderno='+e.order_code
					})
				}else if(e.type=='report'){
					uni.navigateTo({
						url: '/pagesA/message/chat?isDoc=1&cate=report&orderno='+e.order_code
					})
				}
			}
		}
	}
</script>

<style lang="less">
	@import '@/pages/style.less';
	page{
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
	}
	
	.popup-submit-content{
		image{
			display: block;
			width: 56rpx;
			min-width: 56rpx;
			height: 56rpx;
		}
	}
</style>
